package com.woan.api.idao.dao;

import java.util.List;
import java.util.Map;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.woan.api.idao.SysMessageIDAO;
import com.woan.api.model.SysMessage;
import com.woan.api.vo.VmessageList;


/**
 * A data access object (DAO) providing persistence and search support for
 * SysMessage entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.woan.api.model.SysMessage
 * @author MyEclipse Persistence Tools
 */

public class SysMessageDAO extends HibernateDaoSupport implements SysMessageIDAO{
	//发送消息
	public boolean SaveMessage(SysMessage sysmessage){
		try {
			getHibernateTemplate().save(sysmessage);
			return true;
		} catch (RuntimeException re) {
			throw re;
		}
	}
	//历史列表
	public List<VmessageList> FindHistoryMessageByGroup(String groupid, Integer beginIndex,Integer endIndex)
	{
		StringBuffer  buffer=new StringBuffer("select mid,msg_text,sendtime,msg_type,voicelength as \"voicelength\",sys_admininfo.nickName as \"nickName\",sys_admininfo.userName as \"userName\" from sys_message,sys_admininfo " +
					"where sys_message.suid = sys_admininfo.uid and sys_message.rid = '"+groupid+"' and sys_message.isdel='1' order by sendtime desc ");
		
		System.out.println(buffer.toString());
		try
		{
			Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
					Transformers.aliasToBean(VmessageList.class));
			query.setFirstResult(beginIndex);
			query.setMaxResults(endIndex);
			List<VmessageList> list=query.list();
			
			return list;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	//搜索列表
	public List<VmessageList> FindSerachMessageByCon(String groupid,Integer beginIndex,Integer endIndex,Map<String, String> map)
	{
		StringBuffer  buffer=new StringBuffer("select mid,msg_text,sendtime,msg_type,voicelength ,sys_admininfo.nickName as \"nickName\" ,sys_admininfo.userName as \"userName\" from sys_message,sys_admininfo " +
				"where sys_message.suid = sys_admininfo.uid and sys_message.rid = '"+groupid+"' and sys_message.isdel='1' ");
		if(map.get("fabuname").toString()!=null && !map.get("fabuname").equals(""))
			buffer.append(" and sys_admininfo.userName like '%"+map.get("fabuname").toString()+"%' ");
		if(map.get("begintime").toString()!=null && !map.get("begintime").equals("")){
			buffer.append(" and sys_message.sendtime >= '"+ map.get("begintime")+" 00:00:00'");
		}
		if(map.get("endtime").toString()!=null && !map.get("endtime").equals("")){
			buffer.append(" and sys_message.sendtime <= '"+ map.get("endtime")+" 23:59:59'");
		}
		buffer.append(" order by sendtime desc ");
		//System.out.println(buffer.toString());
		try
		{
			Query query = getSession().createSQLQuery(buffer.toString()).setResultTransformer(
					Transformers.aliasToBean(VmessageList.class));
			query.setFirstResult(beginIndex);
			query.setMaxResults(endIndex);
			List<VmessageList> list=query.list();
			
			return list;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	//获取同组内推送消息的用户，type=android，ios
	public List Getpushlist(String rid,String username,String type)
	{
		String sql="";
		if(type.equals("android"))
			//sql="SELECT DISTINCT sys_admininfo.userName,sys_admininfo_mobile.servicecode FROM " +
			//		"sys_admininfo INNER JOIN sys_admininfo_mobile ON sys_admininfo.uid = sys_admininfo_mobile.uid " +
			//		"where sys_admininfo.rid='"+rid+"' and sys_admininfo.userName<>'"+username+"' and sys_admininfo_mobile.pushtype='android' and servicecode is not null";
			sql = "select concat(servicecode,'#',higher,'#',userName) as pushcontent " +
					"from(SELECT DISTINCT sys_admininfo.userName,sys_admininfo.higher," +
					"sys_admininfo_mobile.servicecode FROM sys_admininfo INNER JOIN sys_admininfo_mobile " +
					"ON sys_admininfo.uid = sys_admininfo_mobile.uid where sys_admininfo.rid='"+rid+"' and sys_admininfo.userName<>'"+username+"' and sys_admininfo_mobile.pushtype='android' and servicecode is not null) as a";
		if(type.equals("ios"))
//			sql="SELECT sys_admininfo_mobile.devicetoken FROM " +
//					"sys_admininfo INNER JOIN sys_admininfo_mobile ON sys_admininfo.uid = sys_admininfo_mobile.uid " +
//					"where sys_admininfo.rid='"+rid+"' and sys_admininfo.userName<>'"+username+"' and sys_admininfo_mobile.pushtype='ios' and sys_admininfo_mobile.pushornot = '1'";
			sql = "select concat(servicecode,'#',higher,'#',userName) as pushcontent " +
					"from(SELECT DISTINCT sys_admininfo.userName,sys_admininfo.higher," +
					"sys_admininfo_mobile.servicecode FROM sys_admininfo INNER JOIN sys_admininfo_mobile " +
					"ON sys_admininfo.uid = sys_admininfo_mobile.uid where sys_admininfo.rid='"+rid+"' and sys_admininfo.userName<>'"+username+"' and sys_admininfo_mobile.pushtype='ios' and servicecode is not null) as a";
		System.out.println(sql);
		try
		{
			Query query = getSession().createSQLQuery(sql);
			List list=query.list();
			return list;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	//删除消息
	public boolean DeleteMessage(String messageid)
	{
		Session session=this.getSession();
		StringBuffer hql=new StringBuffer(" update sys_message set isdel='0' where mid='"+messageid+"' ");
		//System.out.println(hql.toString());
		session.createSQLQuery(hql.toString()).executeUpdate();
		return true;
	}
}